// exports.AL_Framework_Event_Center = void 0;
// var i = (function () {
//     function e() {
//         this._postNotificationNames = [];
//         this._dic = new Map();
//     }
//     e.Instance = function () {
//         return this._instance || (this._instance = new e());
//     };
//     e.prototype.emit = function (e, t, o) {
//         if (void 0 === o) {
//             o = !1;
//         }
//         if (null !== e && "" !== e) {
//             if (o) {
//                 if (this._postNotificationNames.indexOf(e) >= 0) {
//                     return;
//                 }
//                 this._postNotificationNames.push(e);
//             }
//             this._huoquShijianWeituo(e).forEach(function (e, o) {
//                 e.forEach(function (e) {
//                     e.call(o, t);
//                 });
//             });
//         }
//     };
//     e.prototype.on = function (e, t, o, i) {
//         if (void 0 === e) {
//             e = null;
//         }
//         if (void 0 === t) {
//             t = null;
//         }
//         if (void 0 === o) {
//             o = null;
//         }
//         if (void 0 === i) {
//             i = !1;
//         }
//         return null == e
//             ? Log.e("Register:name")
//             : null == t
//             ? Log.e("Register:method")
//             : null == o
//             ? Log.e("Register:target")
//             : (this._tianjiaZhiDaoZidian(e, o, t), void (i && t.call(o)));
//     };
//     e.prototype.off = function () {
//         var e = [];
//         for (var t = 0; t < arguments.length; t++) {
//             e[t] = arguments[t];
//         }
//         "string" == typeof e[0] ? this._zhuxiao_1(e[0], e[1], e[2]) : this._zhuxiao_2.apply(this, e);
//     };
//     e.prototype.clear = function () {
//         this._postNotificationNames = [];
//         this._dic = new Map();
//     };
//     e.prototype._huoquShijianWeituo = function (e) {
//         var t = this._huoquZidianZhiHuoMorenzhi(this._dic, e);
//         return null != t ? t : [];
//     };
//     e.prototype._huoquZidianZhiHuoMorenzhi = function (e, t) {
//         return null == e ? null : e.has(t) ? e.get(t) : null;
//     };
//     e.prototype._tianjiaZhiDaoZidian = function (e, t, o) {
//         var i = this._huoquZidianZhiHuoMorenzhi(this._dic, e);
//         if (null == i) {
//             i = new Map();
//             this._dic.set(e, i);
//         }
//         if (null == this._huoquZidianZhiHuoMorenzhi(i, t)) {
//             i.set(t, [o]);
//         } else {
//             {
//                 var n = i.get(t);
//                 n.push(o);
//                 i.set(t, n);
//             }
//         }
//     };
//     e.prototype._zhuxiao_1 = function (e, t, o) {
//         if (null !== e) {
//             return null != o ? this._yichu_teding_zhi(e, t, o) : null != t ? this._yichu_teding_jian(e, t) : this._yichu_teding_jian(e);
//         }
//     };
//     e.prototype._zhuxiao_2 = function (e) {
//         var t = this;
//         if (void 0 === e) {
//             e = null;
//         }
//         return (
//             null != e &&
//             (this._dic.forEach(function (o) {
//                 var i = o;
//                 if (null !== t._huoquZidianZhiHuoMorenzhi(i, e)) {
//                     i.delete(e);
//                 }
//             }),
//             !0)
//         );
//     };
//     e.prototype._yichu_teding_zhi = function (e, t, o) {
//         var i = this._huoquZidianZhiHuoMorenzhi(this._dic, e);
//         if (null == i) {
//             return !1;
//         }
//         if (null == this._huoquZidianZhiHuoMorenzhi(i, t)) {
//             return !1;
//         }
//         var n = i.get(t);
//         n.remove(o);
//         i.set(t, n);
//         return !0;
//     };
//     e.prototype._yichu_teding_jian_1 = function (e) {
//         return this._dic.delete(e);
//     };
//     e.prototype._yichu_teding_jian_2 = function (e, t) {
//         return null != this._huoquZidianZhiHuoMorenzhi(this._dic, e) && this._dic.delete(t);
//     };
//     e.prototype._yichu_teding_jian = function () {
//         var e = [];
//         for (var t = 0; t < arguments.length; t++) {
//             e[t] = arguments[t];
//         }
//         1 === e.length ? this._yichu_teding_jian_1(e[0]) : this._yichu_teding_jian_2(e[0], e[1]);
//     };
//     e._instance = null;
//     return e;
// })();
// exports.AL_Framework_Event_Center = i;


/**
 * 事件中心类
 */
class AL_Framework_Event_Center {
    constructor() {
        /** @type {string[]} */
        this._postNotificationNames = [];
        /** @type {Map<string, Map<object, Function[]>>} */
        this._dic = new Map();
    }

    /**
     * 获取单例
     * @returns {AL_Framework_Event_Center}
     */
    static Instance() {
        if (!this._instance) {
            this._instance = new AL_Framework_Event_Center();
        }
        return this._instance;
    }

    /**
     * 触发事件
     * @param {string} eventName 事件名称
     * @param {*} eventData 事件数据
     * @param {boolean} [isPost=false] 是否只触发一次
     */
    emit(eventName, eventData, isPost = false) {
        if (eventName !== null && eventName !== "") {
            if (isPost && this._postNotificationNames.includes(eventName)) {
                return;
            }
            if (isPost) {
                this._postNotificationNames.push(eventName);
            }
            this._huoquShijianWeituo(eventName).forEach((handlers, context) => {
                handlers.forEach(handler => handler.call(context, eventData));
            });
        }
    }

    /**
     * 注册事件
     * @param {string} eventName 事件名称
     * @param {Function} handler 回调函数
     * @param {object} target 回调函数的目标
     * @param {boolean} [immediate=false] 是否立即执行
     * @returns {void}
     */
    on(eventName = null, handler = null, target = null, immediate = false) {
        if (eventName === null) {
            Log.e("Register:name");
        } else if (handler === null) {
            Log.e("Register:method");
        } else if (target === null) {
            Log.e("Register:target");
        } else {
            this._tianjiaZhiDaoZidian(eventName, target, handler);
            if (immediate) {
                handler.call(target);
            }
        }
    }

    /**
     * 注销事件
     * @param {...any} args 参数列表
     */
    off(...args) {
        if (typeof args[0] === 'string') {
            this._zhuxiao_1(args[0], args[1], args[2]);
        } else {
            this._zhuxiao_2(...args);
        }
    }

    /**
     * 清空所有事件
     */
    clear() {
        this._postNotificationNames = [];
        this._dic = new Map();
    }

    /**
     * 获取事件委托
     * @param {string} eventName 事件名称
     * @returns {Map<object, Function[]>}
     * @private
     */
    _huoquShijianWeituo(eventName) {
        return this._huoquZidianZhiHuoMorenzhi(this._dic, eventName) || new Map();
    }

    /**
     * 获取字典值或默认值
     * @param {Map} dic 字典
     * @param {any} key 键
     * @returns {any}
     * @private
     */
    _huoquZidianZhiHuoMorenzhi(dic, key) {
        if (dic == null) return null;
        return dic.has(key) ? dic.get(key) : null;
    }

    /**
     * 添加值到字典
     * @param {string} eventName 事件名称
     * @param {object} target 目标对象
     * @param {Function} handler 处理函数
     * @private
     */
    _tianjiaZhiDaoZidian(eventName, target, handler) {
        let handlersMap = this._huoquZidianZhiHuoMorenzhi(this._dic, eventName);
        if (!handlersMap) {
            handlersMap = new Map();
            this._dic.set(eventName, handlersMap);
        }
        let handlers = this._huoquZidianZhiHuoMorenzhi(handlersMap, target) || [];
        handlers.push(handler);
        handlersMap.set(target, handlers);
    }

    /**
     * 注销单个事件
     * @param {string} eventName 事件名称
     * @param {object} target 目标对象
     * @param {Function} handler 处理函数
     * @returns {boolean}
     * @private
     */
    _zhuxiao_1(eventName, target, handler) {
        if (eventName !== null) {
            if (handler != null) {
                return this._yichu_teding_zhi(eventName, target, handler);
            } else if (target != null) {
                return this._yichu_teding_jian(eventName, target);
            } else {
                return this._yichu_teding_jian(eventName);
            }
        }
    }

    /**
     * 注销多个事件
     * @param {object} target 目标对象
     * @returns {boolean}
     * @private
     */
    _zhuxiao_2(target = null) {
        if (target != null) {
            this._dic.forEach(handlersMap => {
                if (this._huoquZidianZhiHuoMorenzhi(handlersMap, target) != null) {
                    handlersMap.delete(target);
                }
            });
        }
        return true;
    }

    /**
     * 移除特定事件值
     * @param {string} eventName 事件名称
     * @param {object} target 目标对象
     * @param {Function} handler 处理函数
     * @returns {boolean}
     * @private
     */
    _yichu_teding_zhi(eventName, target, handler) {
        const handlersMap = this._huoquZidianZhiHuoMorenzhi(this._dic, eventName);
        if (!handlersMap) return false;

        const handlers = this._huoquZidianZhiHuoMorenzhi(handlersMap, target);
        if (!handlers) return false;

        const index = handlers.indexOf(handler);
        if (index >= 0) {
            handlers.splice(index, 1);
            if (handlers.length === 0) {
                handlersMap.delete(target);
            } else {
                handlersMap.set(target, handlers);
            }
            return true;
        }
        return false;
    }

    /**
     * 移除特定键
     * @param {string} eventName 事件名称
     * @returns {boolean}
     * @private
     */
    _yichu_teding_jian_1(eventName) {
        return this._dic.delete(eventName);
    }

    /**
     * 移除特定键值
     * @param {string} eventName 事件名称
     * @param {object} target 目标对象
     * @returns {boolean}
     * @private
     */
    _yichu_teding_jian_2(eventName, target) {
        const handlersMap = this._huoquZidianZhiHuoMorenzhi(this._dic, eventName);
        if (handlersMap) {
            return handlersMap.delete(target);
        }
        return false;
    }

    /**
     * 根据参数移除键
     * @param {...any} args 参数列表
     * @returns {boolean}
     * @private
     */
    _yichu_teding_jian(...args) {
        if (args.length === 1) {
            return this._yichu_teding_jian_1(args[0]);
        } else if (args.length === 2) {
            return this._yichu_teding_jian_2(args[0], args[1]);
        }
    }
}

AL_Framework_Event_Center._instance = null;

exports.AL_Framework_Event_Center = AL_Framework_Event_Center;
